<?php 
	require_once("BaseObj.php");
	
	class TaskTime extends BaseObj {
		public $TaskTimeId = 0;
		public $TaskId = 0;
		public $UserId = 0;
		public $Hours = 0;
		public $Description = "";
		public static $Select = "SELECT TaskTimeId, TaskId, UserId, Hours, Description, to_char(CREATED,'MM/DD/YYYY HH24:MI') CREATED, to_char(MODIFIED,'MM/DD/YYYY HH24:MI') MODIFIED FROM TaskTime";
		
		public function delete() {
			$sql = "DELETE FROM TASKTIME WHERE TaskTimeId = :ttid";
			$conn = createConn();
			$stmt = oci_parse($conn, $sql);
			oci_bind_by_name($stmt, ':ttid', $this->TaskTimeId);
			$cnt = oci_execute($stmt);
			oci_free_statement($stmt);
			oci_close($conn);
		}
		
		public static function getByTaskTimeId($tasktimeid){
			$sql = TaskTime::$Select." WHERE TaskTimeId = :tasktimeid";
			$conn = createConn();
			$stmt = oci_parse($conn, $sql);
			oci_bind_by_name($stmt, ':tasktimeid', $tasktimeid);

			$cnt = oci_execute($stmt);
			$u = null;
			if($cnt > 0) {
				$row = oci_fetch_array($stmt, OCI_ASSOC+OCI_RETURN_NULLS);				
				$u = new TaskTime();
				$u->load($row);
			}			
			oci_free_statement($stmt);
			oci_close($conn);
			
			return $u;
		}
		
		public static function getForTask($taskid){ 			
			$arr = array();
			$i = 0;
			$sql = TaskTime::$Select." WHERE TaskId = :taskid";
					
			$conn = createConn();
			$stmt = oci_parse($conn, $sql);
			oci_bind_by_name($stmt, ':taskid', $taskid);
			$cnt = oci_execute($stmt);
			$u = null;
			if($cnt > 0) {				
				while($row = oci_fetch_array($stmt, OCI_ASSOC+OCI_RETURN_NULLS)) {
					$u = new TaskTime();
					$u->load($row);
					$arr[$i] = $u;
					$i++;
				}
			}			
			oci_free_statement($stmt);
			oci_close($conn);
			
			return $arr;
		}
		
		public static function getForTaskAndUser($taskid, $userid){ 			
			$arr = array();
			$i = 0;
			$sql = TaskTime::$Select." WHERE TaskId = :taskid AND UserId = :userid";
					
			$conn = createConn();
			$stmt = oci_parse($conn, $sql);
			oci_bind_by_name($stmt, ':taskid', $taskid);
			oci_bind_by_name($stmt, ':userid', $userid);
			$cnt = oci_execute($stmt);
			$u = null;
			if($cnt > 0) {				
				while($row = oci_fetch_array($stmt, OCI_ASSOC+OCI_RETURN_NULLS)) {
					$u = new TaskTime();
					$u->load($row);
					$arr[$i] = $u;
					$i++;
				}
			}			
			oci_free_statement($stmt);
			oci_close($conn);
			
			return $arr;
		}
		
		public static function getFromURL(){
			$id = null;
			$obj = null;
			if(isset($_GET["tasktimeid"])){ 			
				$id = $_GET["tasktimeid"];
				$obj = TaskTime::getByTaskTimeId($id);
			}
			return $obj;
		}
		
		public function load($row){		
			$this->TaskTimeId = $row["TASKTIMEID"];
			$this->TaskId = $row["TASKID"];
			$this->UserId = $row["USERID"];
			$this->Hours = $row["HOURS"];
			$this->Description = $row["DESCRIPTION"];
			$this->Created = new DateTime($row["CREATED"]);
			$this->Modified = new DateTime($row["MODIFIED"]);
		}
		
		public static function nextId() {
			$sql = "SELECT seq.nextval FROM Dual"; 
			$conn = createConn();
			$stmt = oci_parse($conn, $sql);
			oci_execute($stmt);
			$row = oci_fetch_array($stmt, OCI_ASSOC+OCI_RETURN_NULLS);
			$v = $row["NEXTVAL"];
			oci_free_statement($stmt);
			oci_close($conn);
			return $v;
		}
		
		public function save() { 
			$sql = null;
			if($this->TaskTimeId == null || $this->TaskTimeId == 0) {
				$this->TaskTimeId = TaskTime::nextId();
				$sql = "INSERT INTO TaskTime (TaskTimeId, TaskId, UserId, Hours, Description, Created, Modified) 
				        VALUES (:TaskTimeId, :TaskId, :UserId, :Hours, :Description, :modd, :modd)";	
			}
			else {  				
				$sql = "UPDATE TaskTime
						SET TaskId = :TaskId,
							UserId = :UserId,
							Hours = :Hours, 
							Description = :Description,
							Modified = :modd
						WHERE TaskTimeId = :TaskTimeId";	
			}
			
			$f = $this->nowStr();
			$conn = createConn();
			$stmt = oci_parse($conn, $sql);
			oci_bind_by_name($stmt, ':TaskTimeId', $this->TaskTimeId);
			oci_bind_by_name($stmt, ':TaskId', $this->TaskId);
			oci_bind_by_name($stmt, ':UserId', $this->UserId);
			oci_bind_by_name($stmt, ':Hours', $this->Hours);
			oci_bind_by_name($stmt, ':Description', $this->Description);
			oci_bind_by_name($stmt, ':modd', $f);
			$cnt = oci_execute($stmt);
			oci_free_statement($stmt);
			oci_close($conn);
			
			return $cnt > 0;
		}
	}
?>